package com.yuexun.beilunpatient.ui.inspect.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.yuexun.beilunpatient.R;
import com.yuexun.beilunpatient.base.BaseKJActivity;
import com.yuexun.beilunpatient.config.AppConfig;
import com.yuexun.beilunpatient.ui.inspect.adapter.InspectAdapter;
import com.yuexun.beilunpatient.ui.inspect.bean.CheckResp;
import com.yuexun.beilunpatient.ui.inspect.bean.JtRmyyCheckResult;
import com.yuexun.beilunpatient.ui.inspect.model.impl.InspectModel;
import com.yuexun.beilunpatient.ui.inspect.presenter.impl.InspectPresenter;
import com.yuexun.beilunpatient.ui.inspect.ui.view.IInspectView;
import com.yuexun.beilunpatient.widget.EmptyLayout;

import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;
import org.kymjs.kjframe.utils.SystemTool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chenss on 2018/10/11.
 */
public class Act_Inspect_ScanList extends BaseKJActivity implements IInspectView {
    @Bind(R.id.list)
    ListView listView;
    @Bind(R.id.empty_layout)
    EmptyLayout emptyLayout;

    private InspectPresenter presenter;

    @Override
    public void setRootView() {
        setContentView(R.layout.act_inspect);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        presenter = new InspectPresenter(this, new InspectModel());

        initList();
        ApiGetData();
    }

    /**
     * 初始化列表
     */
    private void initList() {
        emptyLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                emptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
                ApiGetData();
            }
        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                CheckResp item = (CheckResp) adapterView.getItemAtPosition(i);

                if (item.getBarcode() != null) {
                    Bundle bundle = new Bundle();
                    bundle.putString("check_no", item.getCheck_no());
                    Intent intent = new Intent(aty, Act_Inspect_ScanDetail.class);
                    intent.putExtras(bundle);
                    startActivity(intent);
                } else {
                    ViewInject.toast("未出报告单!");
                }
            }
        });
    }

    /**
     * 请请求数据
     */
    private void ApiGetData() {
        if (SystemTool.checkNet(aty)) {
            Map<String, String> params = new HashMap<>();
            params.put("cardNo", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.ID_CARD));
            params.put("patientName", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.USER_NAME));
            presenter.inquireBlrmyyCheckPageList(params);
        } else {
            ViewInject.toast("网络异常，请检查网络是否连接!");
        }
    }

    @OnClick(R.id.title_back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void showInspectList(ArrayList<JtRmyyCheckResult> list) {
        if (list.size() > 0) {
            InspectAdapter adapter = new InspectAdapter(listView, list.get(0).getCheckResps(), R.layout.item_inspectlist);
            listView.setAdapter(adapter);
            emptyLayout.dismiss();
        } else {
            emptyLayout.setErrorType(EmptyLayout.NODATA);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
